import { useReactive } from "ahooks";
import { useModel } from "umi";
import compress from "gql-compress";
import { message } from "antd";

import useCheckStaffPhone from "./useCheckStaffPhone";
import useCheckStaffMember from "./useCheckStaffMember";

const useStaffChangeMember = () => {
    const { graphqlFetch } = useModel("global");
    const { checkStaffPhone } = useCheckStaffPhone();
    const { checkStaffMember } = useCheckStaffMember();

    const state = useReactive({
        loading: false,
    });

    const staffChangeMember = async (staff, member, callback) => {
        if (!member.omPhon) return message.warning("成员没有手机号，不能绑定");
        if (!staff.omUuid) return message.warning("该员工没有绑定成员，不能变更");

        // 检查成员是否已绑定，手机号是否已在员工表里

        let res = await checkStaffMember({ omUuid: member.omUuid, notEqualTo: staff.osUuid });
        if (res === null) return;
        if (res > 0) return message.warning("当前成员已被绑定，不能重复绑定！");

        res = await checkStaffPhone({ osPhon: member.omPhon, notEqualTo: staff.osUuid });
        if (res === null) return;
        if (res > 0) return message.warning("手机号已被某员工使用，不能绑定");

        let variables = {
            osUuid: staff.osUuid,
            omUuid: member.omUuid,
            osName: member.omName,
            pyName: pinyin(member.omName.trim(), { toneType: "none", nonZh: "consecutive" }),
            osRoge: member.omRoge,
            osPhon: member.omPhon,
            osNati: member.omNati,
            osIdnu: member.omIdnu,
            osBirt: member.omBirt,

            oldOmUuid: staff.omUuid,
        };

        let query = staff.omUuid === member.omUuid ? newQuery.replace("#changeOldMember", "") : newQuery.replace("#changeOldMember", changeOldMember);

        res = await graphqlFetch({
            query,
            variables,
        });
        res && callback && callback(res);
        state.loading = false;
    };

    return { ...state, staffChangeMember };
};

let newQuery = compress(`mutation MyMutation(
    $osUuid: UUID!
    $omUuid: UUID!
    $osName: String!
    $pyName: String!
    $osRoge: String!
    $osPhon: String
    $osNati: String
    $osIdnu: String
    $osBirt: Date

    $oldOmUuid: UUID!
  ) {
    updateOrganizationStaffByOsUuid(
        input: {
            osUuid: $osUuid
            organizationStaffPatch: {
                omUuid: $omUuid
                osName: $osName
                pyName: $pyName
                osRoge: $osRoge
                osPhon: $osPhon
                osNati: $osNati
                osIdnu: $osIdnu
                osBirt: $osBirt
                osFrom: "来自成员"
            }
        }
    ) {
        clientMutationId
    }

    updateOrganizationMemberByOmUuid(
        input: {
            omUuid: $omUuid
            organizationMemberPatch: {
                omIfim: true
            }
        }
    ) {
        clientMutationId
    }

    #changeOldMember
}`);

const changeOldMember = `old: updateOrganizationMemberByOmUuid(
    input: {
        omUuid: $oldOmUuid
        organizationMemberPatch: {
            omIfim: false
        }
    }
) {
    clientMutationId
}`;

export default useStaffChangeMember;
